package calculate24.play.SelfCalculate;

import java.util.ArrayList;
import java.util.Random;

import calculate24.play.common.calculate24.Calculate24;
import calculate24.play.common.card.Poker;

//随机生成有解的一组卡片
class ValidInput {
    public final int[] numbers = new int[4];
    public final int[] imageIDs = new int[4];
    public final ArrayList<String> resultSet;

    public ValidInput() {
        int[] values = new int[] {1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,1,2,3,4,5,6,7,8,9,10,11,12,13,};
        int[] indexArray = new int[values.length];
        for(int i = 0; i < values.length; ++i) {
            indexArray[i] = i;
        }
        int[] input = new int[4];
        do {
            shuffle(indexArray);
            input[0] = values[indexArray[0]];
            input[1] = values[indexArray[1]];
            input[2] = values[indexArray[2]];
            input[3] = values[indexArray[3]];
        } while (!isValid(input));
        for(int i = 0; i < 4; ++i) {
            this.numbers[i] = values[indexArray[i]];
            this.imageIDs[i] = Poker.all[indexArray[i]];
        }
        this.resultSet = new Calculate24(input).resultSet;
    }

    private static boolean isValid(int[] input) {
        return new Calculate24(input).resultSet.size() > 0;
    }

    private static void shuffle(int[] o) {
        Random random = new Random();
        for(int i = o.length - 1; 0 < i; --i) {
            int j = random.nextInt(i + 1);
            int tmp = o[i];
            o[i] = o[j];
            o[j] = tmp;
        }
    }

}
